home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / solaris2 / jdk / src / java / awt / test / addremov.jav next >
Encoding:
Text File  |  1995-10-30  |  3.1 KB  |  128 lines

  1. /*
  2.  * @(#)AddRemoveTest.java    1.2 95/09/19 Herb Jellinek
  3.  *
  4.  * Copyright (c) 1995 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. import java.awt.*;
  21. import java.applet.*;
  22.  
  23.  
  24. /**
  25.  * An interactive test of Container.add() and Container.remove().
  26.  *
  27.  * @version 1.2, 09/19/95
  28.  * @author Herb Jellinek
  29.  */
  30.  
  31. public class AddRemoveTest extends Applet {
  32.     /* set atTop to true to start with floatingButtons at top; false puts
  33.      * 'em on bottom.
  34.      */
  35.     boolean atTop = false;
  36.  
  37.     Panel topHolder;
  38.     Panel bottomHolder;
  39.  
  40.     Panel floatingButtons;
  41.  
  42.     Button flipper;
  43.     Button dumper;
  44.     
  45.     public void init() {
  46.     floatingButtons = new Panel();
  47.     floatingButtons.setLayout(new FlowLayout());
  48.     floatingButtons.add(new Button("one"));
  49.     floatingButtons.add(new Button("two"));
  50.     floatingButtons.add(new Button("three"));
  51.     floatingButtons.add(new Button("four"));
  52.     floatingButtons.add(new Button("five"));    
  53.     
  54.     setLayout(new BorderLayout());
  55.     add("North", topHolder = new Panel());
  56.     Panel center = new Panel();
  57.     add("Center", center);
  58.     center.setLayout(new GridLayout(0, 1));
  59.     center.add(new Label("Center", Label.CENTER));
  60.     center.add(flipper = new Button("Flip!"));
  61.     center.add(dumper = new Button("Show!"));
  62.     center.add(new Label("Panel", Label.CENTER));
  63.     add("South", bottomHolder = new Panel());
  64.  
  65.     if (atTop) {
  66.         topHolder.add(floatingButtons);
  67.     } else {
  68.         bottomHolder.add(floatingButtons);
  69.     }
  70.     }
  71.     
  72.     public void start() {
  73.     flipper.enable();
  74.     }
  75.  
  76.     public void stop() {
  77.     flipper.disable();
  78.     }
  79.  
  80.     public boolean handleEvent(Event e) {
  81.     Object target = e.target;
  82.     
  83.     if (e.id == Event.WINDOW_DESTROY) {
  84.         System.exit(0);
  85.         return true;
  86.     } else if (target == flipper) {
  87.         if (atTop) {
  88.         topHolder.remove(floatingButtons);
  89.         bottomHolder.add(floatingButtons);
  90.         invalidate();
  91.         validate();
  92.         } else {
  93.         bottomHolder.remove(floatingButtons);
  94.         topHolder.add(floatingButtons);
  95.         invalidate();
  96.         validate();
  97.         }
  98.         atTop = !atTop;
  99.         return true;
  100.     } else if (target == dumper) {
  101.         dump(this+"");
  102.         return true;
  103.     }
  104.     return false;
  105.     }
  106.  
  107.     void dump(String label) {
  108.     System.out.println("---------- "+label);
  109.     list();
  110.     System.out.println();
  111.     System.out.println();
  112.     }
  113.     
  114.  
  115.     public static void main(String args[]) {
  116.     Frame f = new Frame("AddRemoveTest");
  117.     AddRemoveTest addRemoveTest = new AddRemoveTest();
  118.  
  119.     addRemoveTest.init();
  120.     addRemoveTest.start();
  121.  
  122.     f.add("Center", addRemoveTest);
  123.     f.resize(300, 300);
  124.     f.show();
  125.     }
  126. }
  127.  
  128.